import { useMessage } from "@/hooks/message";
import { isNavigationFailure, Router } from "vue-router";

export default function createProgressGuard(router: Router) {
  router.beforeEach((to) => {
    if (to.meta.hideProgress) return;
    const { zLoadingBar } = useMessage();
    zLoadingBar.start();
  });

  router.afterEach((to, from, failure) => {
    if (to.meta.hideProgress) return;
    const { zLoadingBar } = useMessage();

    if (isNavigationFailure(failure)) {
      zLoadingBar.error();
    } else {
      zLoadingBar.finish();
    }
  });
}
